package com.itheima.reggin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.Employee;
import com.itheima.reggin.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.ExecutorService;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //员工登陆
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1 页面提交 密码 md5处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2 页面提交 username 查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        // 3 没有查询到 返回登录 失败
        if (emp == null) {
            return R.error("登陆失败");
        }
        // 4 密码对比 不一样 返回登录失败
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        // 5 查看员工状态 禁用 or 启用
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        // 6 登陆成功 给员工 id 存入Session 并返回 登陆成功
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    //员工退出
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        session.invalidate();   //销毁session
        return R.success("退出成功");
    }

    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工信息:{}", employee.toString());

        //设置密码 md5 加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("增加成功");
    }

    //分页 查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //构造器 分页
        Page pageInfo = new Page(page, pageSize);
        //条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    //id 查询员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据id 查询员工信息");
        Employee employee = employeeService.getById(id);
        //判断
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有员工信息");
    }

    //根据 id 修改
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());

        long id = Thread.currentThread().getId();
        log.info("线程id为:{}", id);

        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }
}
